package com.xxxx.server.controller;


import com.xxxx.server.base.EmpQuery;
import com.xxxx.server.module.EmpMsg;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.impl.EmployeeServiceImpl;
import com.xxxx.server.utils.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {

    @Resource
    private EmployeeServiceImpl employeeService;

    @ApiOperation(value = "获取所有员工信息")
    @GetMapping("/")
    public Map<String,Object> getAllEmployee(EmpQuery empQuery) {
        return employeeService.getAllEmployee(empQuery);
    }

    @ApiOperation(value = "查询职位")
    @GetMapping("/positions")
    public List<Position> selectEmpByPosition() {
        return employeeService.selectPosition();
    }

    @ApiOperation(value = "查询民族")
    @GetMapping("/nations")
    public List<Nation> selectNation() {
        return employeeService.selectNation();
    }

    @ApiOperation(value = "查询政治面貌")
    @GetMapping("/politicsstatus")
    public List<PoliticsStatus> selectPoliticsStatus() {
        return employeeService.selectPoliticsStatus();
    }

    @ApiOperation(value = "查询职称")
    @GetMapping("/joblevels")
    public List<Joblevel> selectJoblevel() {
        return employeeService.selectJoblevel();
    }

    @ApiOperation(value = "查询部门")
    @GetMapping("/deps")
    public List<Department> selectDepartment() {
        return employeeService.selectDepartment();
    }

    @ApiOperation(value = "员工添加")
    @PostMapping("/")
    public RespBean addEmp(@RequestBody Employee employee) {
        if (employeeService.save(employee)) {
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @ApiOperation(value = "查询员工id")
    @GetMapping("/maxWorkID")
    public Map<String,Object> selectWorkId() {
        return employeeService.selectWorkId();
    }

    @ApiOperation(value = "修改员工信息（编辑）")
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee) {
        if (employeeService.updateById(employee)) {
            return RespBean.success("修改成功！");
        }
        return RespBean.error("修改失败！");
    }

    @ApiOperation(value = "删除员工信息")
    @DeleteMapping("/{id}")
    public RespBean deleteEmp(@PathVariable Integer id) {
        if (employeeService.removeById(id)) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "导出员工信息数据")
    @GetMapping("/export")
    public void excelExport(HttpServletResponse response) {
        //得到所有员工信息列表
        final List<Employee> list = employeeService.list();
        //导出工具类
        try {
            ExcelUtils.exportExcel(list,"员工信息表","员工信息表",
                    Employee.class,"员工信息表",response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "导入员工信息数据")
    @PostMapping("/import")
    public void excelInput(@RequestParam("file") MultipartFile file) throws IOException {
        //导入工具类，得到的数据放入list集合中
        List<Employee> list = ExcelUtils.importExcel(file, Employee.class);
        //批量插入
        employeeService.saveBatch(list);
    }


}
